package phoenix.core.helpers;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import phoenix.core.PhoenixManager;
import phoenix.core.base.HelperBase;
import phoenix.core.base.TestSuiteBase;
import phoenix.core.data_models.RedmineIssueDataModel;

import com.taskadapter.redmineapi.RedmineException;
import com.taskadapter.redmineapi.RedmineManager;
import com.taskadapter.redmineapi.bean.Attachment;
import com.taskadapter.redmineapi.bean.Issue;
import com.taskadapter.redmineapi.bean.User;
import com.taskadapter.redmineapi.bean.Watcher;
import com.taskadapter.redmineapi.internal.Transport;
import com.taskadapter.redmineapi.internal.URIConfigurator;
import com.taskadapter.redmineapi.internal.io.MarkedInputStream;

import core.config.Config;

/**
 * Class provides methods for working with Redmine
 */
public class RedmineHelper extends HelperBase
{
	private final RedmineManager redmine;
	private final Transport transport;

	private List<Attachment> p_attachments = new ArrayList<Attachment>();
	RedmineIssueDataModel issue;

	/**
	 * Initializes RedmineHelper
	 * @param p_phoenix PhoenixManager instance
	 */
	public RedmineHelper(PhoenixManager p_phoenix)
	{
		super(p_phoenix);

		issue = new RedmineIssueDataModel(p_phoenix);
		redmine = new RedmineManager(phoenix.config().REDMINE_HOST, phoenix.config().REDMINE_API_ACCESS_KEY);
		transport = new Transport(new URIConfigurator(phoenix.config().REDMINE_HOST, phoenix.config().REDMINE_API_ACCESS_KEY), null);
	}

	/**
	 * Creates a new issue
	 * @param p_issue RedmineIssueDataModel instance
	 */
	public void createNewIssue(RedmineIssueDataModel p_issue)
	{
		log().debug("Create a new issue in Redmine");

		try
		{
			redmine.createIssue(this.getProjectKey(), p_issue.getIssueData());
			log().debug("Issue was created successfully in Redmine");
		}
		catch (RedmineException p_ex)
		{
			log().warn("Cannot create a new issue: " + p_ex);
		}
	}

	/**
	 * Gets issue by id
	 * @param p_issueID Issue ID
	 * @return Issue instanse
	 */
	public Issue getIssueByID(int p_issueID)
	{
		log().debug("Get issue from Redmine by id: " + p_issueID);
		Issue issue = null;

		try
		{
			issue = redmine.getIssueById(p_issueID);
		}
		catch (RedmineException p_ex)
		{
			log().warn("Issue not found in Redmine: " + p_ex);
		}

		return issue;
	}

	/**
	 * Gets issue by id
	 * @param p_issueID Issue ID
	 * @return Issue instanse
	 */
	public boolean isIssuePresent(int p_issueID)
	{
		if (getIssueByID(p_issueID) != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Is issue present
	 * @param p_issueSubject Issue subject
	 * @return Issue present or not
	 */
	public boolean isIssuePresent(String p_issueSubject, String p_substringInSubject)
	{
		if (getIssuesBySubSubject(p_issueSubject, p_substringInSubject).size() > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Gets issue by id
	 * @param p_issueID Issue ID
	 * @return Issue opened or not
	 */
	public boolean isIssueOpened(int p_issueID)
	{
		int status = getIssueByID(p_issueID).getStatusId();

		if (status != config().getRedmineStatusID("closed") &&
			status != config().getRedmineStatusID("canceled"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Gets first opened issue from the list
	 * @param p_issues List of issues
	 * @return First opened issue
	 */
	public Issue getFirstOpenedIssueFromList(List<Issue> p_issues)
	{
		for (int i = 0; i < p_issues.size(); i++)
		{
			if (isIssueOpened(p_issues.get(i).getId()))
			{
				return p_issues.get(i);
			}
		}

		return null;
	}

	/**
	 * Gets issue by subject
	 * @return Issues list instanses
	 */
	public List<Issue> getIssuesBySubSubject(String p_subject, String p_substringInSubject)
	{
		log().debug("Get issues list from Redmine by subsubject: " + p_subject + ", substring: " + p_substringInSubject);
		List<Issue> issues = null;
		List<Issue> issuesBySubject = new ArrayList<Issue>();

		try
		{
			issues = redmine.getIssuesBySummary(this.getProjectKey(), p_subject);

			if (issues.size() > 0)
			{
				for (int i = 0; i < issues.size(); i++)
				{
					if (issues.get(i).getSubject().contains(p_substringInSubject))
					{
						try
						{
							issuesBySubject.add(issues.get(i));
						}
						catch (Exception p_ex)
						{
							log().info(p_ex.toString());
						}
					}
				}
			}
		}
		catch (RedmineException p_ex)
		{
			log().warn("Issue not found in Redmine: " + p_ex);
		}

		return issuesBySubject;
	}

	/**
	 * Looking for Issues by Status / "new", "closed" etc.
	 * @param p_issueStatus
	 * @return
	 */
	public List<Issue> getIssuesByStatus(String p_issueStatus)
	{
		log().debug("Get issues list from Redmine by status: " + p_issueStatus);
		List<Issue> issues = null;

		try
		{
			Map<String, String> pParameter = new HashMap<String, String>();
			pParameter.put("status_id", p_issueStatus);
			pParameter.put("project_id", this.getProjectKey());
			issues = redmine.getIssues(pParameter);

		}
		catch (Exception p_ex)
		{
			log().warn("Something went wrong in searching by status " + p_ex.toString());
		}

		return issues;
	}

	public List<Issue> getIssuesByDate(String p_date)
	{
		log().debug("Get issues list from Redmine by date : " + p_date.toString());
		List<Issue> issues = null;

		try
		{
			Map<String, String> pParameter = new HashMap<String, String>();
			pParameter.put("start_date", p_date);
			pParameter.put("project_id", this.getProjectKey());
			issues = redmine.getIssues(pParameter);

		}
		catch (Exception p_ex)
		{
			log().warn("Something went wrong in searching by status " + p_ex.toString());
		}
		return issues;
	}

	/**
	 * status_id->Статус
	 * tracker_id->Трекер
	 * priority_id->Приоритет
	 * author_id->Автор
	 * assigned_to_id->Назначена
	 * member_of_group->Группа назначенного
	 * assigned_to_role->Роль назначенного
	 * subject->Тема
	 * created_on->Создано
	 * updated_on->Обновлено
	 * closed_on->Закрыта
	 * start_date->Начата
	 * due_date->Дата выполнения
	 * estimated_hours->Оценка времени
	 * done_ratio->Готовность
	 * is_private->Частная
	 * watcher_id->Наблюдатель
	 */
	public List<Issue> getIssuesBy(Map<String, String> pParameters)
	{
		List<Issue> issues = null;
		Map<String, String> params = new HashMap<String, String>();
		try
		{
			for (final Map.Entry<String, String> param : pParameters.entrySet())
			{
				params.put(param.getKey(), param.getValue());
			}

			issues = redmine.getIssues(params);
		}
		catch (Exception p_ex)
		{
			log().warn("Something went wrong in searching by given params " + p_ex.toString());
		}
		return issues;
	}

	public List<Issue> getIssuesByTrackerId(String p_issueTrackerId)
	{
		log().debug("Get issues list from Redmine by tracker ID : " + p_issueTrackerId);
		List<Issue> issues = null;

		try
		{
			Map<String, String> pParameter = new HashMap<String, String>();
			pParameter.put("tracker_id", p_issueTrackerId);
			pParameter.put("project_id", this.getProjectKey());
			issues = redmine.getIssues(pParameter);

		}
		catch (Exception p_ex)
		{
			log().warn("Something went wrong in searching by status " + p_ex.toString());
		}

		return issues;
	}

	public Issue addNote(Issue p_issue, String p_text)
	{
		log().debug("Add notes to issue in Redmine: " + "issueID=" + p_issue.getId() + "; text=" + p_text);
		p_issue.setNotes(p_text);
		return p_issue;
	}

	public Issue setStatus(Issue p_issue, Integer p_statusId)
	{
		log().debug("Set issue status: " + "issueID=" + p_issue.getId() + "; p_statusId=" + p_statusId);
		p_issue.setStatusId(p_statusId);
		return p_issue;
	}

	public Issue setWatcher(Issue p_issue, int p_watcherId)
	{
		log().debug("Set issue status: " + "issueID=" + p_issue.getId() + "; p_watcherId=" + p_watcherId);
		// Set watcher
		Watcher p_watcher = new Watcher();
		p_watcher.setId(p_watcherId);
		List<Watcher> p_watchers = new ArrayList<Watcher>();
		p_watchers.add(p_watcher);
		// Update issue with new watcher
		p_issue.setWatchers(p_watchers);
		return p_issue;
	}

	public Issue setUserAssignTo(Issue p_issue, int p_userId)
	{
		// Set user assign to
		User p_user = new User();
		p_user.setId(p_userId);
		p_issue.setAssignee(p_user);
		return p_issue;
	}

	/**
	 * Adds attachment
	 * @param p_filePath Attachment file path
	 * @param p_fileName Attachment file name
	 * @param p_contentType Attachment file content type
	 */
	public Issue addAttachment(Issue p_issue, String p_filePath, String p_fileName, String p_contentType)
	{
		log().debug("Add attachment to issue in Redmine: " + "issueID=" + p_issue.getId() + "; text=" + p_filePath);
		Attachment attachment = getAttachment(p_filePath, p_fileName, p_contentType);
		p_issue.getAttachments().add(attachment);
		return p_issue;
	}

	/**
	 * Updates an issue in Redmine
	 * @param p_issue Issue instance
	 */
	public void updateIssue(Issue p_issue)
	{
		log().debug("Update issue in Redmine: " + p_issue.getId());

		try
		{
			redmine.update(p_issue);
		}
		catch (RedmineException p_ex)
		{
			log().warn("Cannot update the issue in Redmine: " + p_ex);
		}
	}

	/**
	 * Gets issue attachment
	 * @param p_filePath Attachment file path
	 * @param p_fileName Attachment file name
	 * @param p_contentType Attachment file content type
	 * @return Attachment instance
	 */
	protected Attachment getAttachment(String p_filePath, String p_fileName, String p_contentType)
	{
		Attachment attachment = new Attachment();
		String token = null;
		InputStream content = null;
		InputStream wrapper = null;

		try
		{
			content = new FileInputStream(p_filePath);
			wrapper = new MarkedInputStream(content, "uploadStream");
			token = transport.upload(wrapper);
		}
		catch (RedmineException p_ex)
		{
			p_ex.printStackTrace();
		}
		catch (FileNotFoundException p_ex)
		{
			p_ex.printStackTrace();
		}

		attachment.setToken(token);
		attachment.setContentType(p_contentType);
		attachment.setFileName(p_fileName);

		return attachment;
	}

	/**
	 * @return the p_attachments
	 */
	public List<Attachment> getAttachments()
	{
		return p_attachments;
	}

	/**
	 * @return the p_attachments
	 */
	public void addAttachment(String p_filePath, String p_fileName, String p_contentType)
	{
		Attachment attachment = getAttachment(p_filePath, p_fileName, p_contentType);
		p_attachments.add(attachment);
	}

	public void clearAttachments()
	{
		p_attachments = new ArrayList<Attachment>();
	}

	/**
	 * Post errors to Redmine
	 */
	public void postError()
	{
		String subject = "[" + testModel().getPlatform() + "]" + "[" + cronos().getCurrentDate("yyyy-MM") + "]" + "[" + TestSuiteBase.TEST_PHASE_NAME + "]";
		String description = "*Test info*\n"
			+ "|Browser name:|" + testModel().getBrowserName() + "|\n"
			+ "|OS:|" + testModel().getOS() + "|\n"
			+ "|Platform:|" + testModel().getPlatform() + "|\n"
			+ "|Site URL:|" + testModel().getSiteURL() + "|\n"
			+ "|User ID:|" + phoenix.testUser().getId() + "|\n"
			+ "|User Autologin:|" + phoenix.testUser().getAutologin() + "|\n"
			+ "|User Gender:|" + phoenix.testUser().getGender() + "|\n"
			+ "|User Email:|" + phoenix.testUser().getEmail() + "|\n"
			+ "|User Password:|" + phoenix.testUser().getPassword() + "|\n"
			+ "|User Screenname:|" + phoenix.testUser().getScreenname() + "|\n"
			+ "|User Membership status:|" + phoenix.testUser().getMembershipStatus() + "|\n"
			+ "|Build:|" + phoenix.testUser().getBuild() + "|\n"
			+ "|User Country:|" + phoenix.testUser().getCountry() + "|\n"
			+ "|ScreenSize:|" + phoenix.testModel().getScreenWight() + " x " + phoenix.testModel().getScreenHight() + "|"
			+ "\n"
			+ log().getErrorsAndWarnings();

		// For REL we want different subjects
		if (phoenix.testModel().getBuild().equals(Config.BUILD_REL))
		{
			subject = "[REL]" + subject;
		}

		String assignToUserID = getAssignUserID(testModel().getPlatform(), TestSuiteBase.TEST_PHASE_NAME);
		String assignToWatcherID = getWatcherUserID(testModel().getPlatform());

		// Tracker Automation Testing
		issue.setTrackerById(100)
			.setAssigneeToById(Integer.parseInt(assignToUserID))
			.setSubject(subject)
			.setDescription(description)
			.setAttachments(getAttachments())
			.setWatcher(Integer.parseInt(assignToWatcherID));

		issue.setParentIssueID(this.getParentIssueID());

		if (!isIssuePresent(subject, TestSuiteBase.TEST_PHASE_NAME))
		{
			createNewIssue(issue);
		}
		if (!isIssuePresent(subject, TestSuiteBase.TEST_PHASE_NAME))
		{
			log().error("No Issue has been created");
		}
		else
		{
			List<Issue> issues = new ArrayList<Issue>();
			issues = getIssuesBySubSubject(subject, TestSuiteBase.TEST_PHASE_NAME);
			Issue issue = getFirstOpenedIssueFromList(issues);
			// Ticket ID
			log().info("Redmine issue id: " + issue.getId());
			testModel().setIssueId(issue.getId().toString());

			addNote(issue, description);
			setStatus(issue, 1); // set status "NEW"
			setWatcher(issue, Integer.parseInt(assignToWatcherID));
			setUserAssignTo(issue, Integer.parseInt(assignToUserID));
			addAttachments(issue, getAttachments());
			updateIssue(issue);
		}
	}

	private Issue addAttachments(Issue p_issue, List<Attachment> p_attachments)
	{
		for (Attachment l_attachment : p_attachments)
		{
			p_issue.getAttachments().add(l_attachment);
		}
		return p_issue;
	}

	/**
	 * @return Boolean / TRUE - no fails / FALSE - Test has failed
	 */
	public Boolean checkIssueStatusAtDb(Issue p_issue)
	{
		Boolean result = false;
		Integer p_start = null;
		Integer p_end = null;

		String l_platform = this.getPlatform(p_issue);

		try
		{
			if (p_issue.getSubject() == null)
			{
				log().error("Subject is null, nothing to do with this");
			}

			// Skip REL tickets check
			if (p_issue.getSubject().contains("[REL]"))
			{
				return false;
			}

			try
			{
				p_start = p_issue.getSubject().lastIndexOf("[");
				p_end = p_issue.getSubject().lastIndexOf("]");
			}
			catch (Exception p_ex)
			{
				log().warn("Unable to continue with subject " + p_issue.getSubject());
				return null;
			}

			if (p_start > 0 && p_end > 0)
			{
				String clearSubject = p_issue.getSubject().substring(p_start + 1, p_end);
				return phoenix.db().checkTestStatusForLastHours(clearSubject, 6, l_platform);
			}

		}
		catch (Exception p_ex)
		{
			log().warn("Something went wrong in checking issues in DB " + p_ex.toString());
		}
		return result;
	}

	public void garbageCollector()
	{
		try
		{
			Map<String, String> params = new HashMap<String, String>();
			params.put("project_id", "platformphoenix_chau"); // project id
			params.put("tracker_id", "100"); // (Bug AT) tracker
			params.put("status_id", "1"); // Status New

			// Get list of issues
			List<Issue> issues = getIssuesBy(params);

			for (int i = 0; i < issues.size(); i++)
			{
				if (checkIssueStatusAtDb(issues.get(i)) == true)
				{
					log().info("We are going to update ticket ID " + issues.get(i).getId());
					phoenix.wd().wait(1);
					addNote(issues.get(i), "There are no errors during last 6 hours");
					phoenix.wd().wait(1);
					issues.get(i).setStatusId(10); // Set "Feedback" status
					phoenix.wd().wait(1);
					updateIssue(issues.get(i)); // Update
					phoenix.wd().wait(1); // Some pause for processing request
				}
			}
		}
		catch (Exception p_ex)
		{
			log().warn("Something went wrong in closing tickets" + p_ex.toString());
		}
	}

	public List<String[]> reportCollector()
	{
		List<String[]> issuesMap = new ArrayList<String[]>();

		try
		{
			Map<String, String> params = new HashMap<String, String>();
			java.util.Date yesterday = new java.util.Date();
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DAY_OF_MONTH, -1);
			yesterday = cal.getTime();
			yesterday = new Date(yesterday.getTime());

			params.put("start_date", yesterday.toString()); // Date
			params.put("project_id", "dating-2"); // Phoenix core
			params.put("tracker_id", "1"); // Bug tracker
			params.put("status_id", "*");

			List<Issue> issues = getIssuesBy(params);

			String[] i_record = null;

			for (int i = 0; i < issues.size(); i++)
			{
				String field = issues.get(i).getCustomField("by Mr.QA");

				if (field.equals("1"))
				{
					i_record = new String[] {
						issues.get(i).getId().toString(), issues.get(i).getSubject(), issues.get(i).getProject().toString(), issues.get(i).getStatusName()
					};

					issuesMap.add(i_record);
				}
			}
		}
		catch (Exception p_ex)
		{
			log().warn("Something went wrong in getting issues by mark " + p_ex.toString());
		}

		return issuesMap;
	}

	private String getAssignUserID(String p_platform, String p_testPhase)
	{
		if (p_platform.equals(config().PLATFORM_WEB))
		{
			switch (p_testPhase)
			{
				// CriticalTS
				case "registrationTest":
					return phoenix.config().KOLOSKOV;
				case "registrationMaleTest":
					return phoenix.config().KOLOSKOV;
				case "registrationFemaleTest":
					return phoenix.config().KOLOSKOV;
				case "confirmationTest":
					return phoenix.config().KOLOSKOV;
				case "confirmationMaleTest":
					return phoenix.config().KOLOSKOV;
				case "confirmationFemaleTest":
					return phoenix.config().KOLOSKOV;
				case "funnelTest":
					return phoenix.config().KOLOSKOV;
				case "funnelFemaleTest":
					return phoenix.config().KOLOSKOV;
				case "funnelMaleTest":
					return phoenix.config().KOLOSKOV;
				case "loginConfirmedMaleTest":
					return phoenix.config().KOLOSKOV;
				case "loginUnConfirmedMaleTest":
					return phoenix.config().KOLOSKOV;
				case "getUserDataTest":
					return phoenix.config().GOLOVKO;
				case "getUserDataFemaleTest":
					return phoenix.config().GOLOVKO;
				case "photoUploadMotivationMaleTest":
					return phoenix.config().KOLOSKOV;
				case "photoUploadMotivationFemaleTest":
					return phoenix.config().KOLOSKOV;
				case "counterBrowseIncrementTest":
					return phoenix.config().GOLOVKO;
				case "counterBrowseDecrementTest":
					return phoenix.config().GOLOVKO;
				case "counterWinkIncrementTest":
					return phoenix.config().GOLOVKO;
				case "counterWinkDecrementTest":
					return phoenix.config().GOLOVKO;
				case "counterMessageIncrementTest":
					return phoenix.config().GOLOVKO;
				case "counterMessageDecrementTest":
					return phoenix.config().GOLOVKO;
				case "notificationBrowseTest":
					return phoenix.config().GOLOVKO;
				case "notificationMessageTest":
					return phoenix.config().GOLOVKO;
				case "notificationWinkTest":
					return phoenix.config().GOLOVKO;
				case "interactionsTest":
					return phoenix.config().GOLOVKO;
				case "activityWinkTest":
					return phoenix.config().GOLOVKO;
				case "activityBrowseTest":
					return phoenix.config().GOLOVKO;
				case "activityFavoritsTest":
					return phoenix.config().GOLOVKO;
				case "paymentPackageTest":
					return phoenix.config().KOLOSKOV;
				case "paymentFeaturesTest":
					return phoenix.config().KOLOSKOV;
				case "mySettingsTest":
					return phoenix.config().GOLOVKO;
				case "interactionsFemaleTest":
					return phoenix.config().GOLOVKO;

				// MyProfileTS
				case "myProfileSaveDataAfterFunnelTest":
					return phoenix.config().KOLOSKOV;
				case "myProfileUpdateMainInfoTest":
					return phoenix.config().KOLOSKOV;
				case "myProfileUpdatePersonalInfoTest":
					return phoenix.config().KOLOSKOV;
				case "myProfileUpdateLookingForInfoTest":
					return phoenix.config().KOLOSKOV;
				case "myProfileUploadPhotoTest":
					return phoenix.config().KOLOSKOV;
				case "myProfileRemovePhotoTest":
					return phoenix.config().KOLOSKOV;
				case "myProfileViewDetailsOtherUserTest":
					return phoenix.config().KOLOSKOV;

				// MenuTS
				case "userMenuTest":
					return phoenix.config().KOLOSKOV;
				case "searchMenuTest":
					return phoenix.config().KOLOSKOV;
				case "activityMenuTest":
					return phoenix.config().KOLOSKOV;
				case "leftBarNavigationTest":
					return phoenix.config().KOLOSKOV;
				case "topMenuTest":
					return phoenix.config().KOLOSKOV;

				// PaymentRedirectionTS
				case "redirectsLookingForUpgradeBannerTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsFullSizePrimaryPhotoTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsFullSizeAdditionalPhotoTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsFreeCommunicationIconTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsInvisibleModeIconTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsVipInSearchIconTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsHotUserIconTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsSearchOnlineBannerUpgradeTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsSearchNearBannerUpgradeTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsSearchRecentlyBannerUpgradeTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsMyAccountButtonUpgradeTest":
					return phoenix.config().KOLOSKOV;
				case "makeUserPaid":
					return phoenix.config().KOLOSKOV;
				case "redirectsPaidFreeCommunicationIconTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsPaidInvisibleModeIconTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsPaidVipInSearchIconTest":
					return phoenix.config().KOLOSKOV;
				case "redirectsPaidMyAccountButtonUpgradeTest":
					return phoenix.config().KOLOSKOV;

				// CrmTS
				case "welcomeLetterTest":
					return phoenix.config().GOLOVKO;
				case "messageLetterTest":
					return phoenix.config().GOLOVKO;
				case "winkLetterTest":
					return phoenix.config().GOLOVKO;
				case "browseLetterTest":
					return phoenix.config().GOLOVKO;
				case "forgotLetterTest":
					return phoenix.config().GOLOVKO;
				case "passwordReminderRemoveTest":
					return phoenix.config().GOLOVKO;
				case "passwordReminderBillingTest":
					return phoenix.config().GOLOVKO;
				case "matchLetterTest":
					return phoenix.config().GOLOVKO;
				case "changeEmailLetterTest":
					return phoenix.config().GOLOVKO;
				case "resendLetterTest":
					return phoenix.config().GOLOVKO;

				// SearchTS
				case "searchUsersInSearchTest":
					return phoenix.config().GOLOVKO;
				case "searchFunnelFreeTest":
					return phoenix.config().GOLOVKO;
				case "searchFreeTest":
					return phoenix.config().GOLOVKO;
				case "searchProfileFreeTest":
					return phoenix.config().GOLOVKO;
				case "searchPaidTest":
					return phoenix.config().GOLOVKO;

				// MessengerTS
				case "messengerFreeUserRedirects":
					return phoenix.config().GOLOVKO;
				case "redirectsTwoMessagesTest":
					return phoenix.config().GOLOVKO;
				case "recieveChatAtProfileTest":
					return phoenix.config().GOLOVKO;
				case "recieveChatAtSearchTest":
					return phoenix.config().GOLOVKO;
				case "redirectsAnswerChatTest":
					return phoenix.config().GOLOVKO;
				case "redirectsHotUserMessageTest":
					return phoenix.config().GOLOVKO;
				case "redirectsFiveMessagesLimitTest":
					return phoenix.config().GOLOVKO;
				case "messageFreeNotificationTest":
					return phoenix.config().GOLOVKO;
				case "messengerPaidUserAlerts":
					return phoenix.config().GOLOVKO;
				case "redirectsPaidFreeCommunicationMessengerLinkTest":
					return phoenix.config().GOLOVKO;
				case "redirectsPaidSpecialDeliveryMessengerLinkTest":
					return phoenix.config().GOLOVKO;

				// PhotoGalleryTS
				case "openGalleryTest":
					return phoenix.config().GOLOVKO;
				case "photosGalleryTest":
					return phoenix.config().GOLOVKO;
				case "navigationGalleryTest":
					return phoenix.config().GOLOVKO;

				// NaughtyModeTS
				case "searchNaughtyTest":
					return phoenix.config().GOLOVKO;
				case "profileNaughtyTest":
					return phoenix.config().GOLOVKO;
				case "notificationNaughtyTest":
					return phoenix.config().GOLOVKO;
				case "galleryNaughtyTest":
					return phoenix.config().GOLOVKO;
				case "switcherNaughtyTest":
					return phoenix.config().GOLOVKO;

				// AdsBridgeTS
				case "adsBridgeSetCookiesTest":
					return phoenix.config().KOLOSKOV;
				case "adsBridgePreconditionTest":
					return phoenix.config().KOLOSKOV;
				case "adsBridgeRuleOneTest":
					return phoenix.config().KOLOSKOV;
				case "adsBridgeRuleTwoTest":
					return phoenix.config().KOLOSKOV;
				case "adsBridgeRuleThreeTest":
					return phoenix.config().KOLOSKOV;
				case "adsBridgeRuleFourTest":
					return phoenix.config().KOLOSKOV;
				case "adsBridgeRuleFiveTest":
					return phoenix.config().KOLOSKOV;
				case "adsBridgeRuleSixTest":
					return phoenix.config().KOLOSKOV;

				default:
					return phoenix.config().REDMINE_WEB_ISSUE_WATCHER;
			}
		}

		if (p_platform.equals(config().PLATFORM_MOBILE))
		{
			switch (p_testPhase)
			{
				case "registrationMaleTest":
					return phoenix.config().NAGIBIN;
				case "registrationFemaleTest":
					return phoenix.config().NAGIBIN;
				case "funnelMaleTest":
					return phoenix.config().NAGIBIN;
				case "funnelFemaleTest":
					return phoenix.config().NAGIBIN;
				case "loginMaleTest":
					return phoenix.config().NAGIBIN;
				case "loginFemaleTest":
					return phoenix.config().NAGIBIN;
				case "redirectsPpFreeChat":
					return phoenix.config().NAGIBIN;
				case "redirectsToPpFromPopUp":
					return phoenix.config().NAGIBIN;
				case "redirectsPpFiveMessages":
					return phoenix.config().NAGIBIN;
				case "redirectsPpFreeProfile":
					return phoenix.config().NAGIBIN;
				case "redirectsToPpFromSearchNearYou":
					return phoenix.config().NAGIBIN;
				case "redirectsPpFreeSearchOnlineNow":
					return phoenix.config().NAGIBIN;
				case "redirectsToPpFromSearchNewMemebers":
					return phoenix.config().NAGIBIN;
				case "redirectsPpFromHotUser":
					return phoenix.config().NAGIBIN;
				case "redirectsPpPaidTest":
					return phoenix.config().NAGIBIN;
				case "redirectFreeChatToHotUserTest":
					return phoenix.config().NAGIBIN;
				case "redirectFreeChatNotificationFromPopUpTest":
					return phoenix.config().NAGIBIN;
				case "redirectFreeChatReplyToIncomingMessageTest":
					return phoenix.config().NAGIBIN;
				case "redirectFreeChatTwoMessagesTest":
					return phoenix.config().NAGIBIN;
				case "redirectPaidMainMenuBannerTest":
					return phoenix.config().NAGIBIN;
				case "redirectPaidChatMakeMessageFirstToReadTest":
					return phoenix.config().NAGIBIN;
				case "redirectFreeChatReadFullIncomingMessageTest":
					return phoenix.config().NAGIBIN;
				case "redirectFreeProfileViewFullPhotoTest":
					return phoenix.config().NAGIBIN;
				case "redirectFreeChatFiveMessageToDifferentRecepientsTest":
					return phoenix.config().NAGIBIN;
				case "redirectFreeSearchNearYouTest":
					return phoenix.config().NAGIBIN;
				case "paidUserPhotoAndDisplayingInfoTest":
					return phoenix.config().NAGIBIN;
				case "freeUserPhotoAndDisplayingInfoTest":
					return phoenix.config().NAGIBIN;
				case "myProfilePagesTest":
					return phoenix.config().NAGIBIN;
				case "myProfileVerifyInfoFromAnotherUserTest":
					return phoenix.config().NAGIBIN;
				case "mainMenuTest":
					return phoenix.config().NAGIBIN;
				case "paymentFeaturesTest":
					return phoenix.config().NAGIBIN;
				case "paymentPackageTest":
					return phoenix.config().NAGIBIN;
				case "mySettingsPagesTest":
					return phoenix.config().NAGIBIN;

				default:
					return phoenix.config().REDMINE_MOB_ISSUE_WATCHER;
			}
		}

		return "Something went wrong";
	}

	private String getWatcherUserID(String p_platform)
	{
		if (p_platform.equals(config().PLATFORM_WEB))
		{
			return phoenix.config().GOLOVKO;
		}

		if (p_platform.equals(config().PLATFORM_MOBILE))
		{
			return phoenix.config().NAGIBIN;
		}
		return null;
	}

	private String getParentIssueID()
	{
		if (testModel().getPlatform().equals(config().PLATFORM_WEB))
		{
			if (testModel().getBuild().equals(Config.BUILD_LIVE))
			{
				if (testModel().getProjectName().equals(config().SUBPROJECT_ADSBRIDGE))
				{
					return config().REDMINE_WEB_ISSUE_ID_ADSBRIDGE;
				}
				else
				{
					return config().REDMINE_WEB_ISSUE_ID;
				}
			}
			else
			{
				return config().REDMINE_WEB_ISSUE_ID_REL;
			}
		}

		else
		{
			return config().REDMINE_MOB_ISSUE_ID;
		}
	}

	private String getProjectKey()
	{
		if (testModel().getPlatform().equals(config().PLATFORM_WEB))
		{
			if (testModel().getBuild().equals(Config.BUILD_LIVE))
			{
				if (testModel().getProjectName().equals(config().SUBPROJECT_ADSBRIDGE))
				{
					return config().REDMINE_PROJECT_KEY_WEB_ADSBRIDGE;
				}
				else
				{
					return config().REDMINE_PROJECT_KEY_WEB;
				}
			}
			else
			{
				return config().REDMINE_PROJECT_KEY_WEB_REL;
			}
		}
		else
		{
			return config().REDMINE_PROJECT_KEY_MOB;
		}
	}

	private String getPlatform(Issue p_issue)
	{
		if (p_issue.getProject().toString().equals("Mobile"))
		{
			return config().PLATFORM_MOBILE;
		}
		else
		{
			return config().PLATFORM_WEB;
		}
	}
}